Thank you for your tip!. I'll definitely try it out once I get this sorted out.
I now encounter a new challenge (just using corridors and closets):
Code:
typedef struct corredores corrs, *ptr_corr;
typedef struct armarios armrs, *ptr_armr;
struct corredores {
int id;
ptr_armr pont_armario;
ptr_corr prox;
};
struct armarios {
int id;
int nr_prods;
ptr_armr prox;
};
int main()
{
ptr_corr lista = NULL;
lista = carrega_lista(lista);
mostra_lista(lista);
}
ptr_corr carrega_lista(ptr_corr p)
{
int i, nr_cors=0, nr_arms=0;
ptr_corr novo, aux;
FILE *f = fopen(FILENAME, RDMODE);
fread(&nr_cors,sizeof(int),1,f);
fread(&nr_arms,sizeof(int),1,f);
fclose(f);
for(i=0; i < nr_cors; i++){
novo = malloc(sizeof(corrs));
if(novo == NULL)
{
printf("Erro ao alocar memoria!\n");
return p;
}
novo->id = i+1;
novo->pont_armario = NULL;
novo->prox = NULL;
if(p == NULL)
p = novo;
else
{
aux = p;
while(aux->prox != NULL)
aux = aux->prox;
aux->prox = novo;
}
}
carrega_armario(p, nr_arms);
return p;
}
void carrega_armario(ptr_corr p, int nr_armarios)
{
ptr_armr novo;
int j=0;
while(p != NULL)
{
for(j=nr_armarios; j > 0; j--){
novo = malloc(sizeof(armrs));
novo->id = j;
novo->nr_prods = 0;
novo->prox = p->pont_armario;
p->pont_armario = novo;
}
p = p->prox;
}
fclose(f);
}
As you can see the first function creates the nodes that are the corridors and then calls for a second function passing the pointer p of the corridor struct and the number of closets that it was read from fread before.
This second function does pretty much the same thing but for closets, but it's set to save the closet nodes at the beginning of the list and I want it to be at the end (thats why I did that inverted for ). How can I do this?
Also, is there any problem that I'm not returning the pointer to the closet list as I did with corridors?
Thank you!